<?php

namespace app\admin\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Request;
use Webman\Http\Response;

class AuthCheck implements MiddlewareInterface
{
    // 免登录接口列表
    protected array $skip = [
        '/admin/auth/login/index',
    ];

    public function process(Request $request, callable $next): Response
    {
        $path = $request->path();
        if (in_array($path, $this->skip, true)) {
            return $next($request);
        }

        $token = getHeaderToken();
       
        $user  = getJWT($token);
        
        if (!$user) {
            return json(['code' => 401, 'msg' => '未登录'])->withStatus(401);
        }

        // 注入用户信息
        $request->jwt = $user;

        return $next($request);
    }
}